<?php
/**
 * ImportForm class file
 *
 * @author Joachim Werner <joachim.pt.werner@de.gm.com>
 */

/**
 * Class for CSV Import Form Handling
 * 
 * @uses CFormModel
 * @package admin
 * @version $Id: ImportForm.php 3 2011-08-31 07:46:15Z juppwerner@googlemail.com $
 * @copyright 2010 Adam OPEL GmbH
 * @author Joachim Werner <joachim.pt.werner@de.gm.com> 
 * @license (tbd)
 */
class ImportForm extends CFormModel
{
    // {{{ *** Members ***
    public $csvFile;
    public $truncateTable;
    public $csvFieldSeparator;
    public $skipFirstNumLines;
    public $loadNbrOfLines = 0;
    public $importSpec = array();
    public $disableyKeys = 0;
    public $lockTable = 0;
    public $prepareDateAttributesOnSave = 1;

    // }}} 
    // {{{ *** Methods ***
    // {{{ initImportSpec
    public function initImportSpec($specName)
    {
        $importSpecs = array();
        // {{{ FPR Items
        $importSpecs['fprlateitems'] = array(
            'title'             => Yii::t('site','Import FPR Items'),
            'model'             => 'FprLateItem',
            'csvFieldSeparator' => ';',
            'truncateTable'     => 1,
            'skipFirstNumLines' => 1,
            'loadNbrOfLines'    => '',
            'importInstructions' => array('/wikiPage/view','id'=>1),
            'redirect'          => '/fprLateItem/admin',
            'additionalLinks'  => array(
                CHtml::link(Yii::t('site','Assign Labor Codes'),array('/admin/import/fprLateItemLaborCode'),array('confirm'=>Yii::t('site',"Really start assigning FPR's to Labor Codes?")))
            ),
            'columns'           => array(
                1 => 'displayIssueNbr',
                2 => 'title',
                3 => 'country',
                4 => 'countryDescription',
                5 => 'engineeringSource',
                6 => 'vehicleLine',
                7 => 'totalCasesAllCountries',
                8 => 'issueStatus',
                9 => 'issueWorkflowStatus',
                10 => 'issueStepChampionFullName',
                11 => 'issueStepChampionDepartment',
                12 => 'originatorFullName',
                13 => array('originationDate','$model->dateISOtoTimestamp($value)'),
                14 => 'laborCodes',
                15 => array('issueClosedDate','$model->dateISOtoTimestamp($value)'),
                16 => 'fprLeadtime',
                17 => 'typeOfAssociation',
                18 => array('associatedTime','$model->dateISOtoTimestamp($value)'),
                19 => 'associatedIssueNbr',
                20 => 'step',
                21 => 'status',
                22 => 'modelYears',
                23 => 'fieldRemedy',
                24 => 'productionRemedy',
            ),
        ); // }}}
        // {{{ TOP 25 IPTV/CC
        $importSpecs['top25iptvcc'] = array(
            'title'             => Yii::t('site','Import TOP 25 IPTV/CC'),
            'model'             => 'Top25IptvCc',
            'csvFieldSeparator' => ',',
            'truncateTable'     => 1,
            'skipFirstNumLines' => 1,
            'redirect'          => '/top25IptvCc/top',
            'columns'           => array(
                0 => 'model_year',
                1 => 'country_code',
                3 => 'model',
                4 => array('labor_code','substr($value,-strlen($value)+3)'),
                5 => 'lc_desc',
                6 => 'sales',
                7 => 'production',
                8 => 'cases',
                9 => 'iptv',
                10 => 'claim_part_tot_rgnl_amt',
                11 => 'claim_misc_tot_rgnl_amt',
                12 => 'claim_lbr_tot_rgnl_amt',
                13 => 'rpt_tot_rgnl_amt',
                14 => 'cost_per_car'
            ),
        ); // }}} 
        // {{{VehicleBuyback
        $importSpecs['vehiclebuyback'] = array(
            'title'		=> Yii::t('site','Import Vehicle Buyback'),
            'model'		=> 'VehicleBuyback',
            'csvFieldSeparator'	=> ',',
            'truncateTable'	=> 1,
            'skipFirstNumLines'	=> 1,
            'redirect'		=> '/vehicleBuyback',
            'columns'		=> array(
                0 => 'Count_Vin_by_Credit_Memo_Date',
                1 => 'Div_Name',
                2 => 'Div_Cd',
                3 => 'Procsg_Prd',
                4 => 'Claim_Category_Nm',
                5 => 'Main_Dlr_Cd',
                6 => 'Rep_Dlr_Cd',
                7 => 'Rep_Dlr_Nm',
                8 => 'Rep_Dlr_Zone_Cd',
                9 => 'Rep_Dlr_Dist_Cd',
                10 => 'Rep_Arsa_Lvl',
                11 => 'Rep_Mechncl_Labour_Rt',
                12 => 'Rep_Paint_Labour_Rt',
                13 => array('Rep_Labour_Rt_Efctv_Dt','$model->dateISOtoTimestamp($value)'),
                14 => 'Labour_Rt_Fl',
                15 => 'Intrnl_Modl_Cd',
                16 => 'Intrnl_Modl_Desc',
                17 => 'Model_Detail_Nm',
                18 => 'Model_Nm',
                19 => 'Vehicle_Type',
                20 => 'Prod_Line_Cd',
                21 => 'Vehicle_Ident_Nbr',
                22 => 'Bad_Vin_Fl',
                23 => array('Warranty_Startup_Dt','$model->dateISOtoTimestamp($value)'),
                24 => array('Prodn_Dt','$model->dateISOtoTimestamp($value)'),
                25 => 'Warranty_Startup_Dlr_Cd',
                26 => 'Warranty_Startup_Div_Cd',
                27 => 'Warranty_Startup_Src_Tp',
                28 => 'Repair_Tm_Mileg_Msr',
                29 => array('Repair_Order_Dt','$model->dateISOtoTimestamp($value)'),
                30 => 'Repair_Order_Nbr',
                31 => 'Doc_Line_Nbr',
                32 => 'Case_Tp',
                33 => 'Scrap_Cd',
                34 => 'Labour_Oprtn_Cd',
                35 => 'Labour_Opt_Desc',
                36 => 'Labour_Opt_Long_Desc',
                37 => 'Net_Itm_Tp',
                38 => 'Failed_Part_Nbr',
                39 => 'Part_Cnt',
                40 => 'Converted_Case_Tp',
                41 => 'Trouble_Cd',
                42 => 'Approval_Code_',
                43 => 'Aprvl_Grp_Desc',
                44 => 'News_Cycle_Cnt',
                45 => array('Run_Dt','$model->dateISOtoTimestamp($value)'),
                46 => array('Credt_Memo_Dt','$model->dateISOtoTimestamp($value)'),
                47 => 'Computed_Norm_Hrs_Amt',
                48 => 'Computed_Othr_Hrs_Amt',
                49 => 'Part_Net_Price_Eur_Amt',
                50 => 'Part_Factry_Eur_Amt',
                51 => 'Part_Markup_Eur_Amt',
                52 => 'Hrs_Pd_Eur_Amt',
                53 => 'Othr_Hrs_Pd_Eur_Amt',
                54 => 'Net_Itm_Eur_Amt',
                55 => 'Computed_Tot_Eur_Amt',
                56 => 'Cost_To_Gm_Eur_Amt',
                57 => 'Computed_Tot_Amt',
                58 => 'Claim_Cnt',
                59 => 'Procsg_Cycle',
                60 => 'Category',
            ),
        ); // }}}
        // {{{ SFI
        $importSpecs['sfi'] = array(
            'title'		=> Yii::t('site','Import Severe Field Issues'),
            'model'		=> 'SfiProblem',
            'csvFieldSeparator'	=> ';',
            'truncateTable'	=> 1,
            'skipFirstNumLines'	=> 1,
            'redirect'		=> '/sfiProblem',
            'columns'		=> array(
                0 => 'model',
                1 => 'legalCaseIdFPR',
                2 => 'vin',
                3 => 'registrationNbr',
                4 => 'engine',
                5 => 'transmission',
                6 => 'acEcc',
                7 => 'options',
                8 => 'fuelType',
                9 => 'caseDate',
                10 => 'mileageKm',
                11 => 'registrationDate',
                12 => 'qrInformDate',
                14 => 'investigationDate',
                15 => 'buildDate',
                16 => 'customerName',
                17 => 'possibleRootcauseNotes',
                18 => 'town',
                19 => 'country',
                20 => 'fprNbr',
            )
        );
        // }}} 
        // {{{ Part Sales
        $importSpecs['part_sales'] = array(
            'title'             => Yii::t('site','Part Sales'),
            'model'             => 'PartSale',
            'csvFieldSeparator' => ';',
            'truncateTable'     => 0,
            'skipFirstNumLines' => 1,
            'redirect'          => '/partSale/admin',
            'prepareDateAttributesOnSave' => 0,
            'columns'           => array(
                0 => 'invoiceDate',
                1 => 'opelOrderNbr',
                2 => 'dealerOrderNbr',
                3 => 'partNbr',
                4 => 'partCatalogNbr',
                5 => 'partQuantity',
                6 => 'dealerNbr',
                7 => 'dealerGroupCode',
                8 => 'countryCode',
                9 => 'acknLocationCode',
                10 => 'partShortName',
                11 => 'engineeringGroupCode',
                12 => 'mainGroup',
                13 => 'productGroup',
                14 => 'productSuperGroup',
                15 => 'partDescription',
                16 => 'modelAppl1',
                17 => 'modelAppl2',
                18 => 'nsc',
                19 => 'dealer',
                20 => 'subDealer'
            ),
        ); // }}} 
        // {{{ Dealer Addresses
        $importSpecs['dealer_addresses'] = array(
            'title'             => Yii::t('site','Import Dealer Addresses'),
            'model'             => 'DealerAddress',
            'csvFieldSeparator' => ';',
            'truncateTable'     => 1,
            'skipFirstNumLines' => 1,
            'redirect'          => '/dealerAddress/admin',
            'columns'           => array(
                0 => 'id',
                1 => 'localDealerId',
                2 => 'dealerName',
                3 => 'strAddrLine1',
                4 => 'strAddrLine2',
                5 => 'postalCode',
                6 => 'city',
                7 => 'phoneNbr',
            ),
        ); // }}} 
        // {{{ Dealer Addresses for Part Sales
        $importSpecs['dealer_addresses_2'] = array(
            'title'             => Yii::t('site','Import Dealer Addresses for Part Sales'),
            'model'             => 'DealerAddress2',
            'csvFieldSeparator' => ';',
            'truncateTable'     => 1,
            'skipFirstNumLines' => 1,
            'prepareDateAttributesOnSave' => 0,
            'redirect'          => '/dealerAddress2/admin',
            'columns'           => array(
                0 => 'dealerNbr',
                1 => 'dealerName',
                2 => 'dealerShortName',
                3 => 'customerType',
                4 => 'dealerAddress1',
                5 => 'dealerAddress2',
                7 => 'dealerCityName',
                8 => 'dealerStateCode',
                9 => 'dealerZipCode',
                10 => 'countryCode',
                11 => 'acknlgLocCode',
                12 => 'fiscalId'
            )
        );
        // }}} 
        // {{{ PRTS
        $importSpecs['prts'] = array(
            'title'             => Yii::t('site','Import PRTS Items'),
            'model'             => 'Prts',
            'csvFieldSeparator' => ';',
            'truncateTable'     => 1,
            'skipFirstNumLines' => 1,
            'loadNbrOfLines'    => 100,
            'prepareDateAttributesOnSave' => 0,
            'redirect'          => '/fprLateItem/admin',
            'columns'           => array(
                0 => 'id',
                1 => 'typeOfAssociation',
                2 => 'associatedIssueNbr',
                3 => 'descriptionPlantContainment',
                4 => 'fieldRemedyFlag',
                5 => 'icaFlag',
                6 => 'containmentActionPlan',
            )
        );
        // }}}                 
        if(array_key_exists($specName,$importSpecs))
            $this->importSpec = $importSpecs[$specName];
        else
            $this->importSpec = array();
    } // }}} 
    // {{{ rules
    public function rules()
    {
        return array(
            array('skipFirstNumLines,csvFieldSeparator','required'),
            array('truncateTable, disableyKeys, lockTable','safe'),
            array('skipFirstNumLines,loadNbrOfLines','numerical'),
            array('csvFile', 'file', 'types'=>'csv'),
        );
    } // }}} 
    // {{{ attributeLabels
    public function attributeLabels()
    {
        return array(
            'csvFile'           => Yii::t('site','CSV File'),
            'csvFieldSeparator' => Yii::t('site','CSV Field Separator'),
            'skipFirstNumLines' => Yii::t('site','Skip the first N lines'),
            'loadNbrOfLines'    => Yii::t('site','Load N Lines'),
            'truncateTable'     => Yii::t('site','Truncate Table'),
            'disableyKeys'      => Yii::t('site','Disable Keys'),
            'lockTable'         => Yii::t('site','Lock Table'),
            'prepareDateAttributesOnSave'
                                => Yii::t('site','Prepare Date Attributes'),
        );
    } // }}} 
    // {{{ dateISOtoTimestamp
    /**
     * Returns a timestamp for an ISO date
     * 
     * @param string $value Date in SO format(YYYY-MM-DD [[hh:mm]|[hh:mm::ss]])
     * @access public
     * @return int Timestamp
     */
    public function dateISOtoTimestamp($value)
    {
        $date = date_parse($value);
        extract($date);
        //Yii::log("value: $value, parsed: $year-$month-$date $hour:$minute:$second");
        // DEBUG echo "<li>year: $year, month: $month, day: $day";
        return mktime($hour,$minute,$second,$month,$day,$year);
    } // }}} 
    // {{{ getCsvFieldSeparatorOptions
    /**
     * Gets an array of CSV Field Separator Options 
     * 
     * @access public
     * @return mixed
     */
    public function getCsvFieldSeparatorOptions()
    {
        return array(
            ','=>Yii::t('site','Colon (,)'),
            ';'=>Yii::t('site','Semi-Colon (;)'),
            '_TAB_'=>Yii::t('site','Tabulator (\\t)'),
        );
    } // }}} 
    // }}} End Methods
}

/* vim:set ai sw=4 sts=4 et fdm=marker fdc=4: */ 
?>
